<!--
 * @Author: 王灿
 * @Date: 2019-12-02 09:25:05
 * @LastEditTime: 2020-03-16 19:46:37
 * @Description: 客户设置vip
 * @FilePath: \scarlet-sparrow-admin\src\views\home\home-customer\customer-vip\index
-->

<template>
  <div class="customer-vip">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm">
      <el-form-item prop="vipType">
        <el-row>
          <el-select v-model="ruleForm.vipType" clearable placeholder="商品类型" @change="onChangeType">
            <el-option
              v-for="item in typeOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
              :disabled="item.disabled"
            ></el-option>
          </el-select>
        </el-row>
      </el-form-item>
      <el-form-item prop="name">
        <el-row>
          <el-input placeholder="礼包名称" v-model="name" clearable readonly></el-input>
        </el-row>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="onSubmitForm('ruleForm')">确 定</el-button>
      <el-button @click="onCancelForm('ruleForm')">取 消</el-button>
    </div>
  </div>
</template>
<script>
import { getByVipType } from '@/api/home.js'
import { getGiftPackageList } from '@/api/commodity.js'
export default {
  name: 'CustomerVip',
  props: {
    row: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data() {
    return {
      name: '',
      ruleForm: {
        userId: '',
        vipType: '',
        vipGiftProperty: ''
      },
      rules: {
        vipType: [{ required: true, message: '请选择', trigger: 'blur' }]
      },
      typeOptions: [
        {
          value: 0,
          label: '普通会员',
          disabled: true
        },
        {
          value: 1,
          label: '超级会员'
        },
        {
          value: 2,
          label: '社群团长'
        },
        {
          value: 3,
          label: '联盟服务商'
        },
        {
          value: 4,
          label: '城市合伙人'
        }
      ]
    }
  },
  watch: {
    row() {
      this.onGetRow()
    }
  },
  created() {
    this.onGetRow()
  },
  methods: {
    // 默认数据
    onGetRow() {
      this.ruleForm.userId = this.row.userId
      this.ruleForm.vipType = this.row.vipLevel
      if (this.ruleForm.vipType) {
        this.onGetGiftPackageList(this.ruleForm.vipType)
      }
    },
    // vip改变
    onChangeType(type) {
      this.onGetGiftPackageList(type)
    },
    // 获取对应礼包名称
    onGetGiftPackageList(type) {
      getGiftPackageList({
        type: type
      }).then( res => {
        this.name = res.data.list[0].name
        this.ruleForm.vipGiftProperty = res.data.list[0].propertyValue
      })
    },
    // 提交
    onSubmitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
           // 编辑
            let form = new FormData
            form.append('userId', this.ruleForm.userId)
            form.append('vipType', this.ruleForm.vipType)
            form.append('vipGiftProperty', this.ruleForm.vipGiftProperty)
            getByVipType(form).then(res => {
              this.$notify.success({
                title: '提示',
                message: '设置成功'
              })
              this.onRefresh()
            })
        }
      })
    },
    // 取消
    onCancelForm(formName) {
      this.$refs[formName].resetFields()
      let data = { isShow: false, isRefresh: false }
      this.$emit('addTableRow', data)
    },
    // 刷新页面
    onRefresh() {
      setTimeout(() => {
        let data = { isShow: false, isRefresh: true }
        this.$emit('addTableRow', data)
      }, 500)
    }
  }
}
</script>

<style lang="scss" scoped>
.customer-vip {
  .el-select {
    width: 100%;
  }
}
</style>